/*
 *  $Id: CardListRegion.cs 1189 2010-11-12 14:40:06Z thenn.erannor $
 *	This file is a part of Ragima CCG.
 *	(C) 2008-2009 Ragima development group.
 *
 *	Licensed under the terms of the GNU GPL License version 2.
 *
 */

using System;
using System.Collections.Generic;

using AgateLib.DisplayLib;
using AgateLib.Geometry;
using AgateLib.InputLib;

using Ragima.Helper;

namespace Ragima.DeckManager {
    public class CardListRegion: Region<DeckManagerScene> {
        private static readonly Rectangle ListRect = new Rectangle(700, 208, 296, 528);
        private const int LineHeight = 16;
        private static readonly int LineCount = ListRect.Height / LineHeight;
        private static readonly Color DelimiterColor = 0xaca394.ToColor();
		private static readonly Color LettersColor = 0x736b5c.ToColor();

        private int _index;
        private List<LineInfo> _data;

        public CardListRegion(DeckManagerScene scene): base(ListRect, scene) {
            _data = new List<LineInfo>(LineCount);
        }

        private int VisibleLines {
            get { return Math.Min(Scene.Cards.Count - _index, LineCount); }
        }

        public void Reset() {
            _index = 0;
            Update();
        }

        public void Update() {
            _data.Clear();
            int max = VisibleLines;
            Size size = new Size(Rect.Width, LineHeight);
            Point pos = new Point(Rect.X, Rect.Y + 2);
            for (int i = 0; i < max; i++) {
                Card card = Scene.Cards[_index+i];
                _data.Add(new LineInfo(card, new Rectangle(pos, size)));
                pos.Y += LineHeight;
            }
        }

        private int GetFocusIndex() {
            Point pos = MousePosition;
            pos.Y -= 2;
            if (pos.Y < 0) return -1;
            int index = pos.Y / LineHeight;
            return index < _data.Count ? index : -1;
        }

        public override Hint GetHint() {
            int i = GetFocusIndex();
            return i != -1 ? _data[i].GetHint() : null;
        }

        public override void Draw() {
            for (int i = 0; i < _data.Count; i++) {
                _data[i].Draw();
                if (i != 0) {
                    Rectangle rect = _data[i].Rect;
                    Display.DrawLine(rect.Left, rect.Top-2, rect.Right, rect.Top-2, DelimiterColor);
                }
            }
        }

        public override void OnClick() {
            int i = GetFocusIndex();
            if (i != -1) {
                Card card = _data[i].Card;
                int count = Keyboard.Keys[KeyCode.Shift] ? card.MaxCount : 1;
                Scene.AddCard(card, count);
            }
        }

        public override void OnRightClick() {
            int i = GetFocusIndex();
            if (i != -1) {
                Scene.RemoveCard(_data[i].Card, Keyboard.Keys[KeyCode.Shift]);
            }
        }

        private struct LineInfo {
            public readonly Card Card;
            public readonly string Name, Weight;
            public readonly Rectangle Rect;
            private Hint _hint;

            public LineInfo(Card card, Rectangle rect) {
                Card = card;
                Name = card.CardName;
                Weight = card.Weight.ToString();
                Rect = rect;
                _hint = null;
            }

            public Hint GetHint() {
                if (_hint == null) _hint = Card.GetHint(Rect);
                return _hint;
            }

            public void Draw() {
                Data.Fonts.Main.Color = LettersColor;
                Data.Fonts.Main.Draw(Rect.Left + 4, Rect.Top, Name);
                Data.Fonts.Main.DrawRight(Rect.Right - 4, Rect.Top, Weight);
            }
        }
    }
}
